/*
 * @lc app=leetcode.cn id=1710 lang=typescript
 *
 * [1710] 卡车上的最大单元数
 */

// @lc code=start
function maximumUnits(boxTypes: number[][], truckSize: number): number {
    boxTypes.sort((a, b) => b[1] - a[1]);

    let count = truckSize;
    let final = 0;
    for (let i = 0; i < boxTypes.length; i++) {
        let [size, num] = boxTypes[i];
        size = Math.min(size, count);
        count -= size;
        final += size * num;
        if (count === 0) break;
    }
    return final;
}
// @lc code=end
